package com.github.maojx0630.translate.core;

import com.github.maojx0630.translate.TranslateConfig;
import com.github.maojx0630.translate.TranslateUtil;
import com.github.maojx0630.translate.cache.ClassFieldCache;
import com.github.maojx0630.translate.translate.dict.DictHandleImpl;
import com.github.maojx0630.translate.translate.dict.DictTranslateHandle;
import com.github.maojx0630.translate.translate.dict.ValueCache;
import com.github.maojx0630.translate.translate.dict.select.DefaultTranslateSelectImpl;
import com.github.maojx0630.translate.translate.dict.select.TranslateSelect;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @author 毛家兴
 **/
public class TranslateConfiguration {

    @Bean
    @ConditionalOnMissingBean(TranslateSelect.class)
    public TranslateSelect translateSelect(SqlSessionFactory sqlSessionFactory, TranslateConfig translateConfig) {
        return new DefaultTranslateSelectImpl(sqlSessionFactory, translateConfig);
    }

    @Bean
    @ConditionalOnMissingBean(ValueCache.class)
    public ValueCache valueCache(TranslateSelect translateSelect, TranslateConfig translateConfig) {
        return new ValueCache(translateSelect, translateConfig);
    }

    @Bean
    @ConditionalOnMissingBean(DictTranslateHandle.class)
    public DictTranslateHandle dictTranslateHandle(ValueCache valueCache, TranslateConfig config) {
        return new DictHandleImpl(valueCache, config);
    }

    @Bean
    public TranslateInterceptor translateInterceptor(TranslateUtil translateUtil) {
        return new TranslateInterceptor(translateUtil);
    }

    @Bean
    public ClassFieldCache classFieldCache(ApplicationContext applicationContext) {
        return new ClassFieldCache(applicationContext);
    }

    @Bean
    public TranslateUtil translateUtil(ClassFieldCache classFieldCache) {
        return new TranslateUtil(classFieldCache);
    }

}
